package com.example.libowu.musicplayer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by libowu on 17-12-16.
 * 此自定义画布是为了实现播放歌曲时以绘制圆弧的形式来表示歌曲的当前进度
 */

public class musicProcess extends SurfaceView implements SurfaceHolder.Callback ,Runnable{
    private Canvas canvas;
    private SurfaceHolder surfaceHolder;
    private Paint paint;
    private boolean isStart=true;//开始线程标志,为false时结束线程
    private boolean isWait=false;//线程挂起的标志,为false时线程挂起
    private float nowtime,alltime;//获取歌曲的当前进度及歌曲总时间
    private float p;//计算歌曲进度百分比的值,目的是用此数来计算绘制圆弧时的结束角度
    private float endAngle;//绘制圆弧的结束角度,公式为nowtime/alltime*360.


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public musicProcess(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint=new Paint();
        surfaceHolder=getHolder();
        surfaceHolder.addCallback(this);

        //目的是使非绘制区背景变成透明,露出下一层的view
        setZOrderOnTop(true);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);


        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(6);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        new Thread(this).start();
        isStart=true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        isStart=false;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void paint(){
        canvas=surfaceHolder.lockCanvas();

        //设置为透明,目的是可以看见控件的下一层view
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        //前四个参数代表离画布左上右下距离的值,第五六个参数时开始,结束时绘制的角度,第八个参数代表绘制的图像是否和圆心相连,true为相连,false为不相连,即为单纯的一段圆弧
        canvas.drawArc(6,6,getWidth()-6,getHeight()-6,-90,endAngle,false,paint);
        if(canvas!=null){
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void run() {
        synchronized (this){

            if(isWait){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            while (isStart){
                Log.e("运行","运行");
                    nowtime=MainActivity.mediaPlayer.getCurrentPosition();
                    alltime=MainActivity.mediaPlayer.getDuration();
                    p=nowtime/alltime;
                    endAngle=360F*p;
                    try{
                        paint();
                    }catch (NullPointerException e){}
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
        }
    }

    public void setWait(boolean isWait){
        this.isWait=isWait;
    }
    public void setNotification(){
        this.isWait=false;
        synchronized (this){
            this.notify();
        }
    }
    public boolean getWait(){
        return isWait;
    }
    public void setStart(boolean isStart){
        this.isStart=isStart;
    }
}
